BCryptPasswordEncoder দিয়ে পাসওয়ার্ড এনকোড করা

Java Technologies - স্প্রিং সিকিউরিটি (Spring Security) - Spring Security এবং Password Management
211

BCryptPasswordEncoder হল Spring Security-তে একটি শক্তিশালী পাসওয়ার্ড এনকোডার, যা পাসওয়ার্ড এনক্রিপ্ট করে নিরাপদ করে। এটি bcrypt hashing অ্যালগরিদম ব্যবহার করে, যা প্রতিটি এনক্রিপশনের জন্য একটি অনন্য সাল্ট (salt) তৈরি করে। এর ফলে একই পাসওয়ার্ড বারবার এনক্রিপ্ট করলেও প্রতিবার আলাদা আউটপুট তৈরি হয়।


BCryptPasswordEncoder কেন ব্যবহার করবেন?

  1. সাল্টেড এনক্রিপশন:
    • প্রতিটি পাসওয়ার্ড এনক্রিপশনের জন্য একটি র‍্যান্ডম সাল্ট যোগ করা হয়। এটি পাসওয়ার্ড গেসিং বা রেইনবো টেবিল আক্রমণ প্রতিরোধ করে।
  2. র‍্যান্ডম হ্যাশিং:
    • একই ইনপুট বারবার এনক্রিপ্ট করলেও বিভিন্ন আউটপুট দেয়।
  3. Spring Security এর জন্য স্ট্যান্ডার্ড:
    • Spring Security-তে এটি ডিফল্ট পাসওয়ার্ড এনকোডার হিসেবে ব্যবহৃত হয়।
  4. Work Factor সমর্থন করে:
    • এনক্রিপশনের কষ্টের মাত্রা (strength) নিয়ন্ত্রণ করা যায়।

BCryptPasswordEncoder দিয়ে পাসওয়ার্ড এনকোড করা

BCryptPasswordEncoder ব্যবহার করার ধাপ:

  1. ডিপেন্ডেন্সি যোগ করুন (যদি না থাকে):
Maven:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
Gradle:
implementation 'org.springframework.boot:spring-boot-starter-security'

2. BCryptPasswordEncoder Bean কনফিগার করা

Spring-এ, সাধারণত একটি Bean কনফিগার করা হয়, যাতে এনকোডার বারবার তৈরি করতে না হয়।

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class SecurityConfig {

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

3. পাসওয়ার্ড এনকোড করা এবং যাচাই করা

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class PasswordEncoderExample {

    public static void main(String[] args) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        // পাসওয়ার্ড এনকোড করা
        String rawPassword = "mySecretPassword";
        String encodedPassword = encoder.encode(rawPassword);

        System.out.println("Raw Password: " + rawPassword);
        System.out.println("Encoded Password: " + encodedPassword);

        // পাসওয়ার্ড যাচাই করা
        boolean isPasswordMatch = encoder.matches(rawPassword, encodedPassword);
        System.out.println("Password Match: " + isPasswordMatch);
    }
}

উদাহরণ আউটপুট:

Raw Password: mySecretPassword
Encoded Password: $2a$10$7d8FKM5nFq6bOZsAgPdwNe7lEzE9nKOob.H5QKb5YhkezzMKyLz9W
Password Match: true

Spring Security এর সাথে Integration

UserDetailsService এবং BCryptPasswordEncoder

Spring Security-তে ব্যবহারকারীদের পাসওয়ার্ড এনকোড করতে এটি ব্যবহার করা হয়।

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter {

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("user")
            .password(passwordEncoder().encode("password"))
            .roles("USER")
            .and()
            .withUser("admin")
            .password(passwordEncoder().encode("admin123"))
            .roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/public/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .httpBasic();
    }
}

BCryptPasswordEncoder এর গুরুত্বপূর্ণ ফিচার

  1. Strength Parameter:
    BCryptPasswordEncoder তৈরি করার সময় strength প্যারামিটার সেট করা যায় (ডিফল্ট: 10)। এটি এনক্রিপশনের সময় ব্যয় বাড়ায়।

    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12); // Strength 12
    
  2. Salt এবং Hash ইন্টারনাল ম্যানেজমেন্ট:
    • সাল্ট অটোমেটিক্যালি হ্যাশের সাথে অন্তর্ভুক্ত করা হয়।
  3. Secure Comparison:
    • matches মেথড একটি সিকিউর কম্পারিজন ব্যবহার করে, যা টাইমিং অ্যাটাক প্রতিরোধ করে।

পাসওয়ার্ড এনকোডার টেস্টিং টিপস

  1. প্রতিটি পাসওয়ার্ড এনকোডিং আলাদা হওয়া উচিত, কারণ সাল্ট ভিন্ন হয়।
  2. matches মেথড দিয়ে যাচাই করুন, পাসওয়ার্ডটি সঠিক কিনা।

উপসংহার

BCryptPasswordEncoder একটি নিরাপদ এবং Spring Security-র জন্য আদর্শ পাসওয়ার্ড এনকোডিং সমাধান। এটি পাসওয়ার্ড এনক্রিপশন এবং যাচাইয়ের জন্য স্ট্যান্ডার্ড হিসেবে ব্যবহৃত হয়। অ্যাপ্লিকেশনের সুরক্ষা বাড়াতে BCrypt ব্যবহার করা অত্যন্ত গুরুত্বপূর্ণ।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...